//
//  RootViewController.m
//  wangyi
//
//  Created by dllo on 15/11/22.
//  Copyright (c) 2015年 liyinkai. All rights reserved.
//

#import "RootViewController.h"
#import "OneCollectionViewCell.h"
@interface RootViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) UIView *daohang;
//导航button
@property (nonatomic, retain) UIButton *buttonToutiao;
@property (nonatomic, retain) UIButton *buttonYule;
@property (nonatomic, retain) UIButton *buttonRedian;
@property (nonatomic, retain) UIButton *buttonTiyu;
@property (nonatomic, retain) UIButton *buttonCaijing;
//model
@property (nonatomic, retain) NSMutableArray *shuju;
@end

@implementation RootViewController
-(void)dealloc {
    [_daohang release];
    [_collection release];
    [_shuju release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    NSMutableArray *array1 = [[NSMutableArray alloc] init];
    [WebAsk chuan:Ktoutiao withBlock:^(id result) {
        NSArray *array = [result valueForKey:@"T1348647909107"];
        for (NSDictionary *dic in array) {
            ToutiaoModel *toutiaomodel = [[ToutiaoModel alloc] initWithDic:dic];
            [array1 addObject:toutiaomodel];
        }
        NSLog(@"收货");
        _shuju = array1;
        [_collection reloadData];
    }];
    
    [self.view addSubview:self.daohang];
    [_daohang release];
    [self.view addSubview:self.collection];
    [_collection release];
}

#pragma mark -- collection协议

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OneCollectionViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.wxzm = _shuju;
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
//        请求对应的网络数据
        _shuju = [GetData GetData:Ktoutiao withNum:@"T1348647909107" withCollection:_collection];
//        改变button样式
        [Change ChangeButton:self.daohang withButton:_buttonToutiao];
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width) {
        _shuju = [GetData GetData:Kyule withNum:@"T1348648517839" withCollection:_collection];
        [Change ChangeButton:self.daohang withButton:_buttonYule];
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width * 2) {
        _shuju = [GetData GetData:Kredian withNum:@"T1348649580692" withCollection:_collection];
        [Change ChangeButton:self.daohang withButton:_buttonRedian];
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width * 3) {
        _shuju = [GetData GetData:Ktiyu withNum:@"T1348649079062" withCollection:_collection];
        [Change ChangeButton:self.daohang withButton:_buttonTiyu];
    } else if (scrollView.contentOffset.x == self.view.bounds.size.width * 4) {
        _shuju = [GetData GetData:Kcaijing withNum:@"T1348648756099" withCollection:_collection];
        [Change ChangeButton:self.daohang withButton:_buttonCaijing];
    }
}

#pragma mark -- view控件
-(UICollectionView *)collection {
    if (!_collection) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - 152);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 108, self.view.bounds.size.width, self.view.bounds.size.height - 152) collectionViewLayout:layout];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.pagingEnabled = YES;
        [_collection registerClass:[OneCollectionViewCell class] forCellWithReuseIdentifier:@"OneCollectionViewCell"];
        [_collection release];
    }
    return _collection;
}
-(UIView *)daohang {
    if (!_daohang) {
        self.daohang = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 44)];
        [_daohang release];
        
        _buttonToutiao = [CreatButton CreatButton:0 withColor:(UIColor *)[UIColor redColor] withView:_daohang withTitle:@"头条"];
        [_buttonToutiao addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        _buttonToutiao.titleLabel.font = [UIFont systemFontOfSize:18];
        
        _buttonYule = [CreatButton CreatButton:1 withColor:(UIColor *)[UIColor grayColor] withView:_daohang withTitle:@"娱乐"];
        [_buttonYule addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        
        _buttonRedian = [CreatButton CreatButton:2 withColor:(UIColor *)[UIColor grayColor] withView:_daohang withTitle:@"热点"];
        [_buttonRedian addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        
        _buttonTiyu = [CreatButton CreatButton:3 withColor:(UIColor *)[UIColor grayColor] withView:_daohang withTitle:@"体育"];
        [_buttonTiyu addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
        
        _buttonCaijing = [CreatButton CreatButton:4 withColor:(UIColor *)[UIColor grayColor] withView:_daohang withTitle:@"财经"];
        [_buttonCaijing addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchDown];
    }
    return _daohang;
}
#pragma mark -- button
-(void)button:(UIButton *)sender {
    if (sender.tag == 100) {
//        请求网络数据
        _shuju = [GetData GetData:Ktoutiao withNum:@"T1348647909107" withCollection:_collection];
//        改变collection偏移量
        [_collection setContentOffset:CGPointMake(0, 0) animated:YES];
//        改变button样式
        [Change ChangeButton:self.daohang withButton:_buttonToutiao];
    } else if (sender.tag == 101) {
        _shuju = [GetData GetData:Kyule withNum:@"T1348648517839" withCollection:_collection];
        [_collection setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
        [Change ChangeButton:self.daohang withButton:_buttonYule];
    } else if (sender.tag == 102) {
        _shuju = [GetData GetData:Kredian withNum:@"T1348649580692" withCollection:_collection];
        [_collection setContentOffset:CGPointMake(self.view.bounds.size.width * 2, 0) animated:YES];
        [Change ChangeButton:self.daohang withButton:_buttonRedian];
    } else if (sender.tag == 103) {
        _shuju = [GetData GetData:Ktiyu withNum:@"T1348649079062" withCollection:_collection];
        [_collection setContentOffset:CGPointMake(self.view.bounds.size.width * 3, 0) animated:YES];
        [Change ChangeButton:self.daohang withButton:_buttonTiyu];
    } else if (sender.tag == 104) {
        _shuju = [GetData GetData:Kcaijing withNum:@"T1348648756099" withCollection:_collection];
        [_collection setContentOffset:CGPointMake(self.view.bounds.size.width * 4, 0) animated:YES];
        [Change ChangeButton:self.daohang withButton:_buttonCaijing];
    }
}

#pragma mark -- 协议跳转
-(void)xinhao:(ToutiaoModel *)model {
    NewsViewController *news = [[NewsViewController alloc] init];
    news.model = model;
    [self.navigationController pushViewController:news animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
